package com.eqdd.yiqidian.ui.companyspace

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.BaseActivity
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.LeaderMovableBean
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.http.JsonConverter
import com.eqdd.yiqidian.utils.ToastUtil
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.FlowableBody
import com.shizhefei.mvc.MVCCoolHelper
import io.reactivex.Flowable
import kotlinx.android.synthetic.main.activity_recyclerview_refresh.*

/**
 * Created by jhj on 18-6-9.
 */

class LeaderMovableListActivity : BaseActivity() {
    private var pageNum: Int = 0
    private var mvcHelper: MVCCoolHelper<List<LeaderMovableBean>>? = null
    private var slimAdapterEx: SlimAdapterEx<LeaderMovableBean>? = null
    private var type: Int = 0
    private var companyId = 0

    override fun initBinding() {
        setContentView(R.layout.activity_recyclerview_refresh)
        type = intent.getIntExtra(Config.TYPE, 0)
        initTopTitleBar(View.VISIBLE, "领导活动")
        /* if (type == 0){
             initTopRightText("新增") {
                 val bundle = Bundle()
                 startActivityForResult(LeaderMovableAddActivity::class.java, bundle, Config.ADD)
             }
         }*/

    }

    override fun initData() {
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.addItemDecoration(DividerLineItemDecoration(this))
        slimAdapterEx = SlimAdapterEx.create().register<LeaderMovableBean>(R.layout.list_item_154, SlimInjector<LeaderMovableBean> { data, injector ->
            injector
                    ?.text(R.id.tv_title, data?.title)
                    ?.text(R.id.tv_time, data?.createTime)
        }).attachTo(recyclerView).updateData(arrayListOf<LeaderMovableBean>() as List<LeaderMovableBean>?) as SlimAdapterEx<LeaderMovableBean>?
        if (type == 0) {
            ItemClickSupport.addTo(recyclerView).setOnItemClickListener { _, position, v ->
                val bundle = Bundle()
                bundle.putInt(Config.ID, slimAdapterEx?.getDataItem(position)?.id ?: -1)
                startActivityForResult(LeaderMovableInfoActivity::class.java, bundle, Config.UPDATE)
            }
            companyId = user.companyId
        } else {
            companyId = intent.getIntExtra(Config.COMPANY_ID, 0)
            if (companyId == 0) {
                ToastUtil.showShort("没有数据")
                finish()
            }
        }


        val dataSource = ModelRx2DataSource<LeaderMovableBean>(ModelRx2DataSource.OnLoadSource<LeaderMovableBean> { page, register ->
            pageNum = if (page == 1) 0 else pageNum
            OkGo.post<HttpResult<List<LeaderMovableBean>>>(HttpConfig.BASE_URL + HttpConfig.LEADER_ACTIVITY_LIST)
                    .params("companyId", companyId)
                    .params("page", pageNum)
                    .converter(object : JsonConverter<HttpResult<List<LeaderMovableBean>>>() {
                        override fun test() {
                            super.test()
                        }
                    }).adapt(FlowableBody())
                    .flatMap { result ->
                        if (page == 1) {
                            mvcHelper?.isAutoLoadMore = true
                        }
                        pageNum = result.page
                        Flowable.just<MutableList<LeaderMovableBean>>(if (result.items == null) mutableListOf<LeaderMovableBean>() else result.items.toMutableList())
                    }
        }, 10)

        mvcHelper = MVCCoolHelper(coolRefreshView)
        mvcHelper?.setDataSource(dataSource)
        mvcHelper?.adapter = slimAdapterEx
        mvcHelper?.refresh()


    }

    override fun setView() {

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                Config.ADD,
                Config.UPDATE -> mvcHelper?.refresh()
            }
        }
    }
}
